<?php

namespace App\Admin\Controllers;

use App\Admin\Exports\CallListExport;
use App\Http\Controllers\Controller;
use App\Model\CallBase;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class VideoCallController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('视频话单详情');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new VideoCall());
        // return Admin::grid(VideoCall::class, function (Grid $grid) {
        $where = [
            // ['status', VoiceCall::STATUS_OK]
        ];
        $grid->model()->with('auditLog')->where($where)->orderBy('id', 'desc');
        $grid->id('话单ID');
        $grid->from('话单类型')->display(function ($data) {
            return VideoCall::FROM_TYPE[$data] . ($this->call_mark == 2 ? '女用户呼叫' : '');
        });
        $grid->status('状态')->display(function ($data) {
            return CallBase::STATUS[$data] ?? '-';
        });
        $grid->channel_id('频道ID');
        $grid->fromUser()->user_profile_id('主叫ID');
        $grid->fromUser()->nick_name('用户昵称');
        $grid->toUser()->user_profile_id('被叫ID');
        $grid->toUser()->nick_name('女用户昵称');
        $grid->coin_setting('价格(金币/分钟)');
        $grid->start_time('发起时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->answer_time('接听时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->end_time('结束时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->call_time('通话时长')->display(function ($time) {
            return $time < 10 ? 0 : ceil($time / 60) . '分钟';
        })->totalRow();
        $grid->total_cost('话单费用')->display(function ($data) {
            return "{$data}";
        });
        $grid->caller_cost('拨打方扣除')->display(function ($data) {
            return "{$data}";
        });
        $grid->host_gain('接听方分成')->display(function ($data) {
            return "{$data}";
        });
        $grid->allowance('平台补贴费用')->display(function ($data) {
            return "{$data}";
        });
        $grid->serviceFee()->coin('平台服务费')->display(function ($data) {
            return "{$data}";
        });
        $grid->heartbeat_user('主叫方心跳更新时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->heartbeat_chat('被叫方心跳更新时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', $time);
        });
        $grid->order_id('订单ID（接通才生成）');
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('id', '话单ID');
                $filter->equal('call_mark', '呼叫类型')->select([1 => '用户呼叫', 2 => '天天福利女用户呼叫',3 => '视频约会女用户呼叫']);
                $filter->equal('channel_id', '频道ID');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('fromUser.user_profile_id', '主叫ID');
                $filter->equal('status', '话单状态')->select(VideoCall::STATUS);
                $filter->equal('from', '话单类型')->select(VideoCall::FROM_TYPE);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('toUser.user_profile_id', '被叫ID');
                $filter->between('created_at', '发起时间')->datetime();
            });
        });
        $grid->auditLog()->callback_info('拦截图片')->display(function ($data) {
            if (!$data) return '';

            $callbackInfo = json_decode($data, true);
            if (!isset($callbackInfo['object'])) {
                return '';
            }

            return ComUtil::buidlImgaeHtml($callbackInfo['object']);
        });
        //禁用导出
        // $grid->disableExport();
        $grid->exporter(new CallListExport($grid));
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableAll();
        });
        return $grid;
        // });
    }
}
